﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoMap
{
    /// <summary>
    /// 使用反射进行映射
    /// </summary>
    public class ReflectorMapper
    {
        public static TResult Map<T, TResult>(T entity) where TResult : new()
        {
            if (entity == null) return new TResult();
            var type = entity.GetType();
            var result = new TResult();
            var targetType = result.GetType();
            var properties = type.GetProperties();
            foreach (var property in properties)
            {
                targetType.GetProperty(property.Name)?.SetValue(result, property.GetValue(entity));
            }
            return result;
        }
    }
}
